package model

import (
	"net/http"
	"strings"

	"github.com/gin-gonic/gin"
)

type AuthType int

const (
	Teacher AuthType = iota
	Student
	Admin
)

var authTypeNames = [...]string{
	"老师",
	"学生",
	"管理员",
}

// String 用户鉴权
func (at AuthType) String() string {
	if at < 0 || int(at) >= len(authTypeNames) {
		return "未知角色"
	}
	return authTypeNames[at]
}

// 权限拦截器
func Auth() {

}

type JsonMsg struct {
	Ctx  *gin.Context
	Code int     `json:"code"`
	Msg  string  `json:"msg"`
	Err  *string `json:"err,omitempty"`
	Data any     `json:"data,omitempty"`
}

func (jm *JsonMsg) Succeed() {
	if value := strings.Split(jm.Msg, ":"); len(value) != 0 {
		jm.Msg = value[1]
	}

	jm.Ctx.JSON(http.StatusOK, gin.H{
		"code": jm.Code,
		"msg":  jm.Msg,
		"data": jm.Data,
	})
}

func SuccessReply(ctx *gin.Context, msg string, data any) {
	if value := strings.Split(msg, ":"); len(value) != 0 {
		msg = value[1]
	}

	ctx.JSON(http.StatusOK, gin.H{
		"code": http.StatusBadRequest,
		"msg":  msg,
		"data": data,
	})
}
